function getDateDiff(dateStr) {
        var publishTime = getDateTimeStamp(dateStr) / 1000,
        d_seconds,
        d_minutes,
        d_hours,
        d_days,
        timeNow = parseInt(new Date().getTime() / 1000),
        d,
 
        date = new Date(publishTime * 1000),
        Y = date.getFullYear(),
        M = date.getMonth() + 1,
        D = date.getDate(),
        H = date.getHours(),
        m = date.getMinutes(),
        s = date.getSeconds();
        //小于10的在前面补0
        if (M < 10) {
                M = '0' + M;
        }
        if (D < 10) {
                D = '0' + D;
        }
        if (H < 10) {
                H = '0' + H;
        }
        if (m < 10) {
                m = '0' + m;
        }
        if (s < 10) {
                s = '0' + s;
        }
 
        d = timeNow - publishTime;
        d_days = parseInt(d / 86400);
        d_hours = parseInt(d / 3600);
        d_minutes = parseInt(d / 60);
        d_seconds = parseInt(d);
 
        if (d_days > 0 && d_days < 3) {
                return d_days + '天前';
        } else if (d_days <= 0 && d_hours > 0) {
                return d_hours + '小时前';
        } else if (d_hours <= 0 && d_minutes > 0) {
                return d_minutes + '分钟前';
        } else if (d_seconds < 60) {
                if (d_seconds <= 0) {
                        return '刚刚';
                } else {
                        return d_seconds + '秒前';
                }
        } else if (d_days >= 3 && d_days < 30) {
                return M + '月' + D +'日';
        } else if (d_days >= 30) {
                return Y.toString().slice(2,4) + '年' + M + '月' + D +'日';
        }
}

function getDateTimeStamp(dateStr) {
    // 如果时间格式为2020/07/09 21:43:19.000  需要去掉.000 不然ios和firefox会有问题
	return Date.parse(dateStr.replace(/-/gi, "/"));
}

function formatDate(date) {
  const day = date.getDate().toString().padStart(2, '0');
  const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的
  const year = date.getFullYear();
  const hours = date.getHours().toString().padStart(2, '0');
  const minutes = date.getMinutes().toString().padStart(2, '0');
  const seconds = date.getSeconds().toString().padStart(2, '0');
 
  return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
}

function getDateDiffSecond(dateStr) {
	let publishTime = getDateTimeStamp(dateStr) / 1000;
	let timeNow = parseInt(new Date().getTime() / 1000);
	let d = timeNow - publishTime;
	return parseInt(d);
}

function getDateDiffMinute(dateStr) {
	let publishTime = getDateTimeStamp(dateStr) / 1000;
	let timeNow = parseInt(new Date().getTime() / 1000);
	let d = timeNow - publishTime;
	return parseInt(d/ 60);
}

module.exports = {
	getDateDiff:getDateDiff,
	formatDate:formatDate,
	getDateDiffMinute:getDateDiffMinute
}